/** WPTReporter catch error **/
(function(g,f){typeof exports==='object'&&typeof module!=='undefined'?module.exports=f():typeof define==='function'&&define.amd?define(f):(g=g||self,g.WPTReporter=f());}(this,function(){'use strict';function _classCallCheck(instance, Constructor) {
  if (!(instance instanceof Constructor)) {
    throw new TypeError("Cannot call a class as a function");
  }
}

function _defineProperties(target, props) {
  for (var i = 0; i < props.length; i++) {
    var descriptor = props[i];
    descriptor.enumerable = descriptor.enumerable || false;
    descriptor.configurable = true;
    if ("value" in descriptor) descriptor.writable = true;
    Object.defineProperty(target, descriptor.key, descriptor);
  }
}

function _createClass(Constructor, protoProps, staticProps) {
  if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  if (staticProps) _defineProperties(Constructor, staticProps);
  return Constructor;
}function assign(target) {
  if (target == null) {
    // TypeError if undefined or null
    throw new TypeError('Cannot convert undefined or null to object');
  }

  var to = Object(target);

  for (var index = 0; index < (arguments.length <= 1 ? 0 : arguments.length - 1); index++) {
    var nextSource = index + 1 < 1 || arguments.length <= index + 1 ? undefined : arguments[index + 1];

    if (nextSource != null) {
      // Skip over if undefined or null
      for (var nextKey in nextSource) {
        // Avoid bugs when hasOwnProperty is shadowed
        if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
          to[nextKey] = nextSource[nextKey];
        }
      }
    }
  }

  return to;
}

var WPTReporter =
/*#__PURE__*/
function () {
  function WPTReporter(option) {
    var _this = this;

    _classCallCheck(this, WPTReporter);

    this.report = function (data) {
      var isAlert = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;

      // 只有手机环境才会弹出错误
      if (isAlert && !window.navigator.platform.match(/mac|win/i)) {
        var error = JSON.stringify(data, null, 4);

        var transCssText = function transCssText(styleObj) {
          var keyMap = {
            p: 'position',
            t: 'top',
            l: 'left',
            w: 'width',
            h: 'height',
            o: 'overflow',
            bc: 'backgroundColor',
            br: 'borderRadius',
            pd: 'padding'
          };
          return Object.keys(styleObj).map(function (key) {
            return "".concat((keyMap[key] || key).replace(/([A-Z])/g, '-$1'.toLowerCase()), ":").concat(styleObj[key]);
          }).join(';');
        }; // 弹层样式


        var maskStyle = transCssText({
          p: 'fixed',
          t: 0,
          l: 0,
          w: '100vw',
          h: '100vh',
          bc: 'rgba(0,0,0,.2)',
          zIndex: 999999,
          fontSize: '12px'
        });
        var modalStyle = transCssText({
          p: 'absolute',
          o: 'hidden',
          w: '80vw',
          h: 'auto',
          t: '50%',
          l: '50%',
          transform: 'translate(-50%, -50%)',
          bc: '#ffffff',
          br: '5px',
          zIndex: 1999999
        });
        var contentStyle = transCssText({
          o: 'auto',
          h: 'auto',
          pd: '10px',
          lineHeight: '16px',
          maxHeight: '60vh'
        });
        var btnStyle = {
          border: '1px solid #dfdfdf',
          bc: '#ffffff',
          margin: '0 5px',
          br: '4px',
          pd: '3px 10px'
        }; // 蒙层

        var mask = document.createElement('div');
        mask.style.cssText = maskStyle; // 弹窗

        var modal = document.createElement('div');
        modal.style.cssText = modalStyle; // 头部

        var header = document.createElement('div');
        header.innerText = 'js错误';
        header.style.cssText = transCssText({
          textAlign: 'center',
          bc: 'rgb(243, 243, 243)',
          pd: '10px 0'
        }); // 错误内容

        var content = document.createElement('div');
        content.style.cssText = contentStyle;
        content.innerHTML = error.replace(/\n/g, '<br/>').replace(/(\s+)/g, function (s, t) {
          return '&nbsp;'.repeat(t.length);
        }); // 复制错误使用

        var textarea = document.createElement('textarea');
        textarea.setAttribute('readonly', 'readonly');
        textarea.style.cssText = transCssText({
          p: 'absolute',
          t: '-10000px'
        });
        textarea.value = error; // 底部栏

        var footer = document.createElement('div');
        footer.style.cssText = transCssText({
          pd: '7px 0',
          textAlign: 'center',
          bc: 'rgb(243, 243, 243)'
        }); // 确认按钮

        var confirm = document.createElement('button');
        confirm.innerText = '确定';
        confirm.style.cssText = transCssText(btnStyle);
        confirm.addEventListener('click', function () {
          document.body.removeChild(mask);
        }); // 复制按钮

        var copy = document.createElement('button');
        copy.innerText = '复制';
        copy.style.cssText = transCssText(btnStyle);
        copy.addEventListener('click', function () {
          textarea.select();
          document.execCommand('Copy');
          setTimeout(function () {
            document.body.removeChild(mask);
          }, 500);
        });
        footer.appendChild(confirm);
        footer.appendChild(copy);
        mask.appendChild(modal);
        mask.appendChild(textarea);
        modal.appendChild(header);
        modal.appendChild(content);
        modal.appendChild(footer);
        document.body.appendChild(mask);
      }

      var reportData = assign({}, data, {
        UA: window.navigator.userAgent,
        platform: window.navigator.platform,
        screen: [window.screen.availWidth, window.screen.availHeight].join(','),
        url: window.location.href,
        localTime: Date.now(),
        project: _this.option.project
      });
      reportData = _this.option.onBeforeSend(reportData);

      if (reportData) {
        var _content = encodeURIComponent(JSON.stringify(reportData));

        new Image().src = "".concat(_this.option.api, "?t=").concat(Date.now(), "&content=").concat(_content);
      }
    };

    if (!option.project) {
      console.error('project不能为空');
      return;
    }

    this.option = assign({
      api: 'https://api.weipaitang.com/wechat/v1.0/remind/error-by-front',
      onBeforeSend: function onBeforeSend(data) {
        return data;
      }
    }, option);

    this._addEvent();
  }

  _createClass(WPTReporter, [{
    key: "_addEvent",
    value: function _addEvent() {
      var _this2 = this;

      var errorConfig = assign({
        log: true,
        alert: true,
        limit: 2
      }, this.option.error);
      var reportNum = 1;

      var handleError = function handleError(JSRuntimeError) {
        if (reportNum > errorConfig.limit) {
          return;
        }

        reportNum += 1;

        if (errorConfig.log && JSRuntimeError.stack) {
          console.log('错误栈信息', JSRuntimeError.stack);
        }

        _this2.report({
          JSRuntimeError: JSRuntimeError
        }, errorConfig.alert);
      }; // 获取详细的错误信息


      if (window.addEventListener) {
        window.addEventListener('error', function (messageOrEvent) {
          var _ref = messageOrEvent || {},
              _ref$message = _ref.message,
              message = _ref$message === void 0 ? '' : _ref$message,
              _ref$filename = _ref.filename,
              filename = _ref$filename === void 0 ? '' : _ref$filename,
              _ref$lineno = _ref.lineno,
              lineno = _ref$lineno === void 0 ? 0 : _ref$lineno,
              _ref$colno = _ref.colno,
              colno = _ref$colno === void 0 ? 0 : _ref$colno,
              _ref$error = _ref.error,
              error = _ref$error === void 0 ? {} : _ref$error;

          handleError({
            message: message,
            filename: filename,
            stack: error && error.stack,
            lineno: lineno,
            colno: colno,
            error: error
          });
        }); // 捕获Promise错误

        window.addEventListener('unhandledrejection', function (event) {
          event.promise && event.promise["catch"](function (res) {
            res && handleError({
              message: res.message,
              promiseError: 1,
              reason: event.reason,
              stack: res && res.stack
            });
          });
        });
      } else {
        window.onerror = function (event, source, lineno, colno, error) {
          handleError({
            message: event,
            filename: source,
            stack: error && error.stack,
            lineno: lineno,
            colno: colno,
            error: error
          });
        };
      }
    }
  }]);

  return WPTReporter;
}();return WPTReporter;}));